let house_id = ''
let house_index = ''
Page({
  data: {
    dialogVisible: false,
  },

 async onShow(){
    //页面显示加载房屋列表
    try {
      const res = await wx.http.get('/room')
      this.setData({
        houseList:res.data
      })
    } catch (error) {
      wx.utils.toast('获取列表失败','fail')
    }
  },
  swipeClose(ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      house_id = ev.mark.id
      house_index = ev.mark.index
      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  //点击弹窗事件
  async dialogClose(e){
    console.log(e);
    if (e.detail == 'confirm') {
      //删除该条数据,服务器+本地数组
    const {code} = await wx.http.delete('/room/'+house_id)
    if (code !== 10000) {
      return wx.utils.toast('删除失败','fail')
    }
      this.data.houseList.splice(house_index,1)
      this.setData({
        houseList:this.data.houseList
      })
      wx.utils.toast('删除成功','success')
    }
  },

  goDetail(e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id='+e.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
